<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="字体大小pt、px、em、rem的区别"><meta name="keywords" content="css"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>字体大小pt、px、em、rem的区别 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#详解-px-和-em-的特点和区别"><span class="toc-number">1.</span> <span class="toc-text">详解 px 和 em 的特点和区别</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CSS-中-em-属性有如下特点"><span class="toc-number">2.</span> <span class="toc-text">CSS 中 em 属性有如下特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#所以我们在写-CSS-的时候，需要注意"><span class="toc-number">3.</span> <span class="toc-text">所以我们在写 CSS 的时候，需要注意</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#rem"><span class="toc-number">4.</span> <span class="toc-text">rem</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">字体大小pt、px、em、rem的区别</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-09</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/css/">css</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>这里向大家描述一下<a href="http://developer.51cto.com/art/201008/220838.htm" target="_blank" rel="noopener">CSS</a>中 px 和 em 的特点和区别，px 像素（Pixel），相对长度单位，像素 px 是相对于显示器屏幕分辨率而言的，而 em 是相对长度单位，相对于当前对象内文本的字体尺寸，相信本文介绍一定会让你有所收获。</p>
<a id="more"></a>

<h4 id="详解-px-和-em-的特点和区别"><a href="#详解-px-和-em-的特点和区别" class="headerlink" title="详解 px 和 em 的特点和区别"></a>详解 px 和 em 的特点和区别</h4><p>象素 px 是我们在定义 CSS 中经常用到的尺寸大小单位，而 em 在国外网站中经常被使用，px 和 em 之间究竟有什么区别和特点呢？</p>
<ul>
<li>px 像素（Pixel），相对长度单位。像素 px 是相对于显示器屏幕分辨率而言的。</li>
<li>em 是相对长度单位，相对于当前对象内文本的字体尺寸。如当前对行内文本的字体尺寸未被人为设置，则相对于浏览器的默认字体尺寸。<br>因此用 px 来定义字体，就无法用浏览器字体放大的功能。<br>任意浏览器的默认字体高都是 16px。所有未经调整的浏览器都符合:1em=16px。那么 12px=0.75em,10px=0.625em。为了简化 font-size 的换算，需要在 css 中的 body 选择器中声明 Font-size=62.5%，这就使 em 值变为 16px*62.5%=10px,这样 12px=1.2em,10px=1em,也就是说只需要将你的原来的 px 数值除以 10，然后换上 em 作为单位就行了。</li>
</ul>
<h4 id="CSS-中-em-属性有如下特点"><a href="#CSS-中-em-属性有如下特点" class="headerlink" title="CSS 中 em 属性有如下特点"></a>CSS 中 em 属性有如下特点</h4><ul>
<li>em 的值并不是固定的；</li>
<li>em 会继承父级元素的字体大小。</li>
</ul>
<h4 id="所以我们在写-CSS-的时候，需要注意"><a href="#所以我们在写-CSS-的时候，需要注意" class="headerlink" title="所以我们在写 CSS 的时候，需要注意"></a>所以我们在写 CSS 的时候，需要注意</h4><ul>
<li>body 选择器中声明 Font-size=62.5%；</li>
<li>将你的原来的 px 数值除以 10，然后换上 em 作为单位；</li>
<li>重新计算那些被放大的字体的 em 数值。避免字体大小的重复声明。<br>也就是避免 1.2*1.2=1.44 的现象。<br>比如说你在#content 中声明了字体大小为 1.2em，那么在声明 p 的字体大小时就只能是 1em，而不是 1.2em,因为此 em 非彼 em，它因继承#content 的字体高而变为了 1em=12px。<br>但是 12px 汉字例外，就是由以上方法得到的 12px(1.2em)大小的汉字在 IE 中并不等于直接用 12px 定义的字体大小，而是稍大一点。这个问题 Jorux 已经解决，只需在 body 选择器中把 62.5%换成 63%就能正常显示了。原因可能是 IE 处理汉字时，对于浮点的取值精确度有限。不知道有没有其他的解释。</li>
</ul>
<h4 id="rem"><a href="#rem" class="headerlink" title="rem"></a>rem</h4><p>em 是相对长度单位，相对于当前对象内文本的字体尺寸，即 em 的计算是基于父级元素 font-size 的。比如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span> <span class="attr">style</span>=<span class="string">"font-size:14px"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">"font-size:2em"</span>&gt;</span>我这里的字体显示大小是28px(14px*2)<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">"font-size:18px"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">"font-size:2em"</span>&gt;</span>我这里显示字体大小是36px(18px*2),而不是上面计算的28px<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>rem 是 css3 新增的一个相对单位，与 em 的区别在于，它是相对于 html 根元素的(在 body 标签里面设置字体大小不起作用)。还是上面那个例子，如果换做 rem，结果如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span> <span class="attr">style</span>=<span class="string">"font-size:14px"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">"font-size:2rem"</span>&gt;</span></span><br><span class="line">    我这里的字体显示大小是32px(16px*2),因为我是根据html根元素的font-size大小进行计算的</span><br><span class="line">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">"font-size:18px"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">"font-size:2rem"</span>&gt;</span></span><br><span class="line">      我这里显示字体大小是32px(16px*2),因为我是根据html根元素的font-size大小进行计算的</span><br><span class="line">    <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>** <code>补充默认font-size大小是16px(如果html中没有设置的话)。</code>**</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/09/%E5%AD%97%E4%BD%93%E5%A4%A7%E5%B0%8Fpt%E3%80%81px%E3%80%81em%E3%80%81rem%E7%9A%84%E5%8C%BA%E5%88%AB/">http://blog.wangpengpeng.site/2020/01/09/%E5%AD%97%E4%BD%93%E5%A4%A7%E5%B0%8Fpt%E3%80%81px%E3%80%81em%E3%80%81rem%E7%9A%84%E5%8C%BA%E5%88%AB/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/css/">css</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/09/js%E4%B8%ADthis%E7%9A%84%E6%8C%87%E5%90%91%E9%97%AE%E9%A2%98/"><i class="fa fa-chevron-left">  </i><span>js中this的指向问题</span></a></div><div class="next-post pull-right"><a href="/2020/01/09/css%E4%BC%98%E5%85%88%E7%BA%A7-%E6%9D%83%E9%87%8D%E5%80%BC/"><span>css优先级-权重值</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>